package com.jysz.certificate.web.action.system;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.model.system.Brand;
import com.jysz.certificate.service.system.BrandService;
import org.apache.struts2.convention.annotation.Action;

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/system/brand")
public class BrandAction extends AbstractBaseAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private BrandService brandService;
	private Brand brand;
	private Brand brandDTO;
	private Long id;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Brand getBrand() {
		return brand;
	}
	public void setBrand(Brand brand) {
		this.brand = brand;
	}
	@Action(ADD)
	public String add(){
		setRedirectUrl("index.do?currentPage="+getCurrentPage(),"brandDTO",brandDTO);
		return EDIT;
	}
	/*
	 * 设置默认主机厂
	 */
	@Action("updateTheDefault")
	public String updateTheDefault(){
		brandService.updateThe();
		Brand brand = brandService.find(id);
		brand.setTheDefault(new Integer(0));
		brandService.save(brand);
		
		setRedirectUrl("index.do?currentPage="+getCurrentPage(),"brandDTO",brandDTO);
		setSuccessPopMessage("默认主机厂设置成功");
		return SHOW_MSG_PAGE;
	}
	/*
	 * 增加-修改
	 */
	@Action(EDIT)
	public String edit(){
		Brand brands =new Brand();
		if(brand.getId()!= null){
			brands = brandService.find(brand.getId());
		}
		setRedirectUrl("index.do?currentPage="+getCurrentPage(),"brandDTO",brandDTO);
			String ary[]=new String[]{"id","createdAt","creator","theDefault"};
			BeanUtils.copyProperties(brand,brands, ary);
			brandService.save(brands);
			brand=new Brand();
			setSuccessPopMessage("数据存储成功");
			return SHOW_MSG_PAGE;
	}
	public Brand getBrandDTO() {
		return brandDTO;
	}
	public void setBrandDTO(Brand brandDTO) {
		this.brandDTO = brandDTO;
	}
	@Action(UPDATE)
	public String update(){
		brand=brandService.find(brand.getId());
		setRedirectUrl("index.do?currentPage="+getCurrentPage(),"brandDTO",brandDTO);
		return EDIT;
	}
/*
 * 查询所有
 */
	@Action(INDEX)
	public String index(){
			this.setPage(brandService.indexBrand(brandDTO,this.getCurrentPage(), this.getPageSize()));
		return "index";
	} 
}
